<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\Category;
use Illuminate\View\View;

class AccordionController extends Controller
{
    /**
     * 显示手风琴式三级联动页面
     *
     * @return View
     */
    public function index(): View
    {
        // 获取所有分类及其文章数量
        $categories = Category::withCount(['posts' => function ($query) {
                $query->where('status', 'published')
                      ->whereNotNull('published_at')
                      ->where('published_at', '<=', now());
            }])
            ->whereNull('parent_id')
            ->orderBy('name')
            ->get()
            ->filter(function ($category) {
                return $category->posts_count > 0; // 只显示有文章的分类
            });

        return view('accordion.index', compact('categories'));
    }
}